package com.event.management.frontend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.event.management.frontend.domain.Event;
import com.event.management.frontend.dto.EventListDTO;
import com.event.management.frontend.dto.EventQueryParam;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 活动Mapper接口
 */
@Mapper
public interface EventMapper extends BaseMapper<Event> {

    /**
     * 分页查询活动列表
     *
     * @param page   分页参数
     * @param param  查询条件
     * @return       分页结果
     */
    IPage<EventListDTO> selectEventList(Page<EventListDTO> page, @Param("param") EventQueryParam param);
    
    /**
     * 查询热门推荐活动
     * 根据推荐分数和浏览量的加权值进行排序
     * 推荐分数权重为0.7，浏览量权重为0.3
     *
     * @param limit  查询数量
     * @return       活动列表
     */
    @Select("SELECT e.id, e.title, e.cover, e.start_time AS startTime, e.end_time AS endTime, " +
            "e.location, e.category, e.tags, e.view_count AS viewCount, e.status, " +
            "e.recommend_score AS recommendScore " +
            "FROM events e " +
            "WHERE e.is_deleted = 0 AND e.status = 'online' " +
            "ORDER BY (e.recommend_score * 0.7 + e.view_count * 0.3) DESC, e.created_at DESC " +
            "LIMIT #{limit}")
    List<EventListDTO> selectHotEvents(@Param("limit") Integer limit);
    
    /**
     * 更新活动浏览量
     *
     * @param eventId 活动ID
     * @return        更新结果
     */
    int incrementViewCount(@Param("eventId") Long eventId);
    
    /**
     * 增加活动推荐分数
     *
     * @param eventId 活动ID
     * @param score 要增加的分数
     * @return 更新结果
     */
    @Update("UPDATE events SET recommend_score = recommend_score + #{score} WHERE id = #{eventId}")
    int incrementRecommendScore(@Param("eventId") Long eventId, @Param("score") float score);
} 